package com.jgp.glmeeting.controller;

import com.jgp.glmeeting.model.MeetingParticipantsRelation;
import com.jgp.glmeeting.model.Participants;
import com.jgp.glmeeting.service.MeetingParticipantsRelationService;
import com.jgp.glmeeting.service.ParticipantsService;
import com.jgp.sys.common.controller.JGPController;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import com.jgp.sys.ui.annotation.UIParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/glMeeting/participantsApiController")
public class ParticipantsApiController extends JGPController {
    @Autowired
    private ParticipantsService participantsService;

    @Autowired
    private MeetingParticipantsRelationService meetingParticipantsRelationService;

    /**
     * 进行所有人员查询（分页）
     */
    @RequestMapping("/queryParticipants")
    public Result queryParticipants(Participants p, @UIParam("pager") Pager pager) {
        List<Participants> lists = participantsService.queryParticipants(p, pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("姓名", "name"));
        categories.add(new GridResult.Category("性别", "sex","BUSI.SEX"));
        categories.add(new GridResult.Category("所属单位", "workUnit"));
        categories.add(new GridResult.Category("职务", "job"));
        categories.add(new GridResult.Category("联系方式", "mobilePhone"));
        return ajaxRe("gdata", categories, lists, pager);
    }


    /**
     * 进行剩余人员查询（分页）查询还有哪些人员在该会议没被添加
     */

    @RequestMapping("/queryLastUsers/{meetingID}")
    public Result queryLastUsers(Participants p, @UIParam("pager") Pager pager, @PathVariable String meetingID) {
        List<MeetingParticipantsRelation> meetingParticipantsRelations = meetingParticipantsRelationService.queryUsersIDs(meetingID);
        List<String> userIDs = new ArrayList<>();
        for (int i = 0; i < meetingParticipantsRelations.size(); i++) {
            userIDs.add(meetingParticipantsRelations.get(i).getParticipantsId());
        }
        List<Participants> lists = participantsService.queryLastParticipants(p, pager, userIDs);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("姓名", "name"));
        categories.add(new GridResult.Category("性别", "sex"));
        categories.add(new GridResult.Category("所属单位", "workUnit"));
        categories.add(new GridResult.Category("职务", "job"));
        categories.add(new GridResult.Category("联系方式", "mobilePhone"));
        return ajaxRe("gdata", categories, lists, pager);
    }

    /**
     *  添加人员
     */
    @Transactional
    @RequestMapping("/addParticipants")
    public Result addParticipants(Participants p) {
        participantsService.addParticipants(p);
        return ajaxRe(true);
    }

    /**
     * 删除参会人员
     */
    @Transactional
    @RequestMapping("/removeParticipants")
    public Result removeParticipants(String id,@UIParam("pager") Pager pager) {
//        List<RelationTable> relationTables = relationTableService.queryMeetingIDs(id);
//        for (int i = 0; i < relationTables.size(); i++) {
//            relationTableService.removeJoinUser(relationTables.get(i).getId());
//        }
        participantsService.removeParticipants(id);
        return ajaxRe(true);
    }




}

